package com.ruoyi.web.controller.tool;

import com.ruoyi.domain.FS_TCP_Frame;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TCPClient {
    private static final int PACKET_SIZE = 1490;
    private Socket socket;
    private DataOutputStream out;
    private DataInputStream in;

    public void connect(String host, int port) throws IOException {
        socket = new Socket(host, port);
        out = new DataOutputStream(socket.getOutputStream());
        in = new DataInputStream(socket.getInputStream());
    }

    public void sendRequest(FS_TCP_Frame request) throws IOException {
        out.writeInt(request.FS_HEAD);
        out.writeByte(request.FS_FLAG);
        out.writeByte(request.FS_CHANNEL);
        out.writeInt(request.FS_COUNT);
        out.writeInt(request.FS_N);
        out.writeShort(request.FS_SIZE);
        for (float f : request.FS) {
            out.writeFloat(f);
        }
        out.writeDouble(request.FS_REQUEST_S);
        out.writeDouble(request.FS_REQUEST_E);
        out.writeByte(request.FS_FILE_C);
        out.writeByte(request.FS_FILE_N);
        out.writeInt(request.FS_END);
        out.flush();
    }

    public List<float[]> receiveData() throws IOException {
        Map<Integer, FS_TCP_Frame> packetMap = new HashMap<>();
        int expectedPackets = -1;
        byte[] buffer = new byte[PACKET_SIZE];

        while (true) {
            int bytesRead = in.read(buffer);
            if (bytesRead == -1) {
                break;
            }

            ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
            DataInputStream dis = new DataInputStream(bais);

            FS_TCP_Frame frame = new FS_TCP_Frame();
            frame.FS_HEAD = dis.readInt();
            frame.FS_FLAG = dis.readByte();
            frame.FS_CHANNEL = dis.readByte();
            frame.FS_COUNT = dis.readInt();
            frame.FS_N = dis.readInt();
            frame.FS_SIZE = dis.readShort();

            for (int i = 0; i < 363; i++) {
                frame.FS[i] = dis.readFloat();
            }

            frame.FS_REQUEST_S = dis.readDouble();
            frame.FS_REQUEST_E = dis.readDouble();
            frame.FS_FILE_C = dis.readByte();
            frame.FS_FILE_N = dis.readByte();
            frame.FS_END = dis.readInt();



            packetMap.put((int) frame.FS_FILE_C, frame);

            if (expectedPackets == -1) {
                expectedPackets = frame.FS_FILE_N;
            }

            if (packetMap.size() >= expectedPackets) {
                break;
            }
        }

        // 组合所有数据包
        List<float[]> result = new ArrayList<>();
        for (int i = 1; i <= expectedPackets; i++) {
            FS_TCP_Frame frame = packetMap.get(i);
            if (frame != null) {
                result.add(frame.FS);
            }
        }

        return result;
    }

    public void close() throws IOException {
        if (out != null) {
            out.close();
        }
        if (in != null) {
            in.close();
        }
        if (socket != null) {
            socket.close();
        }
    }
}

